package com.example.dell.newstart.detail;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.dell.newstart.MainActivity;
import com.example.dell.newstart.R;
import com.example.dell.newstart.base.BaseActivity;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.base.ServerResponse;
import com.example.dell.newstart.bean.CartList;
import com.example.dell.newstart.bean.MyApp;
import com.example.dell.newstart.bean.Product;
import com.example.dell.newstart.bean.User;
import com.example.dell.newstart.login.Login;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import q.rorbin.badgeview.QBadgeView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Goods_Activity extends BaseActivity {
    private static final String TAG = "Goods_Activity";
     Product product;


    @BindView(R.id.bg_image)
    ImageView ivProduct;
    @BindView(R.id.de_price)
    TextView tvProductPrice;
    @BindView(R.id.de_name)
    TextView tvProductName;
    @BindView(R.id.tv_cart)
    ImageView tvCart;
    @BindView(R.id.bt_add_to_cart)
    TextView btAddToCart;



    QBadgeView qBadgeView;//显示购物车新增货物数量

    @Override
    public int initLayout() {

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        product = (Product) bundle.get("product");

        return R.layout.activity_goods_detail;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        Glide.with(this)
                .load(RequestInterface.baseImgPath + product.getMainImage())
                .into(ivProduct);
        tvProductName.setText(product.getName());
        tvProductPrice.setText("￥"+product.getPrice() + "");
        //购物车有新商品时，显示新商品数量的小圆点
        qBadgeView = new QBadgeView(this);
        qBadgeView.bindTarget(tvCart);
        qBadgeView.setBadgeNumber(0);
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.ib_back, R.id.tv_home, R.id.tv_cart,  R.id.bt_add_to_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.tv_home:
                goNextActivity(MainActivity.class);
                finish();
                break;
            case R.id.tv_cart:
                qBadgeView.setBadgeNumber(0);

                startActivity(new Intent(Goods_Activity.this, MainActivity.class));
                break;
            case R.id.bt_add_to_cart://加入购物车

                MyApp app = (MyApp) getApplication();
                User user = app.getCurrentUser();
                if(user==null){
                    toastShort("未登录");
                    goNextActivity(Login.class);
                    return;
                }
               AddCartProductListRequest(product.getId(),1);
                Log.d(TAG, "onViewClicked: badge " + qBadgeView + " tvCart " + tvCart);
                int number = qBadgeView.getBadgeNumber() + 1;
                qBadgeView.setBadgeNumber(number);

                //creteOrder(product);
                break;

        }
    }




    //购物车添加
    private void AddCartProductListRequest(int product,int count) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Cart requestInterfaceLogin = retrofit.create(RequestInterface.Cart.class);
        User user2 = MyApp.getCurrentUser();
        if (user2   == null) {
            Toast.makeText(this, "请先登陆", Toast.LENGTH_SHORT).show();
            //context.startActivity(new Intent(context, LoginActivity.class));
            return;
        }
        Call<ServerResponse<CartList>> call = requestInterfaceLogin.add(product,count,user2.getId());

        call.enqueue(new Callback<ServerResponse<CartList>>() {

            @Override
            public void onResponse(Call<ServerResponse<CartList>> call, Response<ServerResponse<CartList>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                if (response.body() == null) {
                    toastShort("未获取到相关数据");
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
//                    CartList cartList = response.body().getData();
//                    if (cartList != null) {
//                        List<CartProduct> cartProductList = cartList.getCartProductVoList();
//                        if (cartProductList != null && cartProductList.size() > 0) {
//                          mcart.clear();
//                            mcart.addAll(cartProductList);//更新适配器数据
//                            notifyDataSetChanged();//通知变化
//                            if (onCartVisibilityChangeListener != null) {
//                                onCartVisibilityChangeListener.onVisibilityChange(true);
//                            }
//                        } else {
//                            if (onCartVisibilityChangeListener != null) {
//                                onCartVisibilityChangeListener.onVisibilityChange(false);
//                            }
//                        }
//                    }
                } else {
                    toastShort("添加失败！");
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<CartList>> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getMessage());
                toastShort("获取相关数据失败！");
            }
        });
    }


}
